# Logger File
# Author: Rishav Jain

# Tags -
# info

from profile import profile
import variables

import threading
import time
import os

LOG_PATH = './logs/'

if not os.path.exists(LOG_PATH):
    os.makedirs(LOG_PATH)

TIME = time.time()
logFile = open(LOG_PATH + time.asctime(time.gmtime(TIME+19800)), 'w+')

queue = ()

def writeLog(TAG, MSG):
    global TIME, logFile
    
    if time.time()-TIME > 3600:
        TIME = time.time()
        logFile.close()
        logFile = open(LOG_PATH + time.asctime(time.gmtime(TIME+19800)), 'w+')

    logFile.write(MSG)
    logFile.write('\n')
    logFile.flush()
    
    print 'Writing ' + MSG + ' to File'
    return

def logService():
    global queue
    
    while True:
        if len(queue) > 0:
            tmp = queue
            queue = ()
            for i in tmp:
                writeLog(i[0], i[1])

        time.sleep(10)

def log(TAG, MSG):
    global queue
    
    queue += ((TAG, MSG),)
    print queue

def logInit():
    LogTask = threading.Thread(target = logService, args=())
    LogTask.start()

##i=0
##while True:
##    log('info', str(i))
##    i+=1
##    time.sleep(1)
